<template>
  <div>
    <el-row ><el-col align="center"><el-button thpe="primary" @click="insertNews">添加新闻</el-button></el-col></el-row>
  <el-table :data="pageInfo.list" border  align="center">
    <el-table-column type="selection" width="100"></el-table-column>
    <el-table-column type="index" label="序号" ></el-table-column>
    <el-table-column prop="title" label="标题"></el-table-column>
    <el-table-column prop="content" label="内容"></el-table-column>
    <el-table-column prop="author" label="作者"></el-table-column>
    <el-table-column prop="createDate" label="上传时间"></el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="danger" @click="deleteNews(scope.row)">删除</el-button>
        <el-button type="primary" @click="updateNews(scope.row)">修改</el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination align="center" layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="total"
        :page-size.sync="pageSize"
        :page-sizes="pageSizes"
        :current-page.sync="currentPage"
        :page-count="pageCount"
         background
         @size-change="handSize"
         @current-change="handPage"
    >
    </el-pagination>
  </div>
</template>

<script>
    export default {
      name: "ShowNews",
       data(){
        return{
          total:1,
          pageSizes:[1,2,3,4,5],
          pageSize:3,
          pageNum:1,
          currentPage:1,
          pageCount:5,
          pageInfo:{}
        }
       },
      methods:{
        insertNews(){
          this.$router.push({path:'insertNews'})
        },
        handSize(size){
          this.pageSize=size
        },
        handPage(page){
          this.pageNum=page
          this.selectNews()
        },
        updateNews(news){
          this.$router.push({path:'/updateNews',query:{news:news}})
        },
        deleteNews(news){
          this.axios({
            method:"delete",
            url:"/news/"+news.id,
          }).then((res)=>{
            alert("删除成功")
            this.selectNews()
          })
        },
        selectNews(){
          this.axios({
            method:"get",
            url:"/new",
            params:{
              pageSize:this.pageSize,
              pageNum:this.pageNum
            }
          }).then((res)=>{
            this.pageInfo=res.data
            this.total=this.pageInfo.total
          })
        }
      },
      created() {
       this.selectNews();
      }
    }
</script>
<style scoped>
</style>
